package com.guli.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.edu.entity.Course;
import com.guli.edu.entity.EduTeacher;
import com.guli.edu.entity.fuzzy.TeacherLike;
import com.guli.edu.service.CourseService;
import com.guli.edu.service.EduTeacherService;
import com.guli.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ZhaoZhenyu
 * @since 2022-09-20
 */

//@CrossOrigin    // 跨域
@Api(description = "讲师管理")  // swagger用
@RestController
@RequestMapping("/edu/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "查询所有讲师")
    @GetMapping("/getAllTeacher")
    public R getAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "根据Id删除讲师")
    @DeleteMapping("/removeTeacherById/{id}")
    public R removeTeacherById(
            @ApiParam(name = "id",value = "讲师Id",required = true)
            @PathVariable String id){       // id是string
        boolean flag = eduTeacherService.removeById(id);
        if (flag) {
            return R.ok();
        }else {
            return R.error().message("删除失败");
        }
    }

    @GetMapping("/getTeacherByPage/{current}/{size}")
    public R getTeacherByPage(@PathVariable Integer current,@PathVariable Integer size){
        Page<EduTeacher> eduTeacherPage = new Page<>(current, size);
        eduTeacherService.page(eduTeacherPage, null);

        List<EduTeacher> records = eduTeacherPage.getRecords();     // 得到当前页所有老师
        long total = eduTeacherPage.getTotal();                     // 得到总条数
        return R.ok().data("records",records).data("total",total);
    }

    @PostMapping("/getTeacherByLikePage/{current}/{size}")
    public R getTeacherByLikePage(@PathVariable Integer current,@PathVariable Integer size,
                                  @RequestBody TeacherLike teacherLike){      // @RequestBody接收json数据 post请求
        Page<EduTeacher> eduTeacherPage = new Page<>(current,size);
        eduTeacherService.pageQuery(eduTeacherPage,teacherLike);

        List<EduTeacher> records = eduTeacherPage.getRecords();
        long total = eduTeacherPage.getTotal();
        return R.ok().data("records",records).data("total",total);
    }

    @PostMapping("/saveTeacher")
    public R saveTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.save(eduTeacher);
        if (flag) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @GetMapping("/getTeacherById/{id}")
    public R getTeacherById(@PathVariable String id){
//        int i = 10 / 0;
        /*if (true) {
            throw new GuliException(20001, "自定义异常");
        }*/
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("item", eduTeacher);
    }

    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 前台分页
    @GetMapping("/teacherPageOfFront/{page}/{limit}")
    public R teacherPageOfFront(@PathVariable Long page,@PathVariable Long limit){
        Page<EduTeacher> pageParam = new Page<>(page,limit);
        Map<String,Object> map = eduTeacherService.getPageOfFront(pageParam);
        return R.ok().data(map);
    }

    // 前台详情页
    @GetMapping("/InfoById/{id}")
    public R getById(@PathVariable String id){
        //查询讲师信息
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        //根据讲师id查询这个讲师的课程列表
        List<Course> courseList = courseService.list(new QueryWrapper<Course>().eq("teacher_id", id));
        return R.ok().data("teacher", eduTeacher).data("courseList", courseList);
    }

}

